package com.gimi.cloud.jimi.manage.controller.sys;

import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.utils.JsonUtil;
import com.gimi.cloud.jimi.common.utils.reqhead.Response;
import com.gimi.cloud.jimi.common.utils.reqhead.Util;
import com.gimi.cloud.jimi.dto.sys.SysMenuDTO;
import com.gimi.cloud.jimi.dto.params.MenuSingleParams;
import com.gimi.cloud.jimi.manage.service.sys.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by wangwei on 2017/12/15.
 */
@RestController
@RequestMapping(value = "menu")
public class MenuController {
    private final MenuService menuService;

    @Autowired
    public MenuController(MenuService menuService) {
        this.menuService = menuService;
    }

    /**
     * 菜单查询操作
     *
     * @param menuDTO
     * @param request
     * @param response
     */
    @RequestMapping(value = "/list")
    public void menuList(@RequestBody SysMenuDTO menuDTO, HttpServletRequest request, HttpServletResponse response) {
        //System.out.println(request.getContentType()+"=====");
        //System.out.println(request.getHeaderNames().toString()+"======");
        Response resp = new Response();
       // List<SysMenuDTO> list = menuService.getMenuListByPid(menuDTO);
        List<SysMenuDTO> list = menuService.getMenuListReturnAll();
        resp.setRes_data(list);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJSONStringWithDateFormatYYYYMMDDHHMMSS(resp), response);
    }

    @RequestMapping(value = "/single")
    public void single(@RequestBody MenuSingleParams params, HttpServletRequest request, HttpServletResponse response) {
        //System.out.println(request.getContentType()+"=====");
        //System.out.println(request.getHeaderNames().toString()+"======");
        Response resp = new Response();
        SysMenuDTO data = menuService.getMenuSingle(params);
        resp.setRes_data(data);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }

    /**
     * 菜单添加操作
     *
     * @param menuDTO
     * @param request
     * @param response
     */
    @RequestMapping(value = "/add")
    public void menuAdd(@RequestBody SysMenuDTO menuDTO, HttpServletRequest request, HttpServletResponse response) {
        Response resp = new Response();
        if (menuService.menuAdd(menuDTO)) {
            resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        } else {
            resp.setResponseCode(ResponseCodeEnum.FAILURE);
        }
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }

    /**
     * 菜单更新操作
     *
     * @param menuDTO
     * @param request
     * @param response
     */
    @RequestMapping(value = "/update")
    public void menuUpdate(@RequestBody SysMenuDTO menuDTO, HttpServletRequest request, HttpServletResponse response) {
        Response resp = new Response();
        if (menuService.menuUpdate(menuDTO)) {
            resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        } else {
            resp.setResponseCode(ResponseCodeEnum.FAILURE);
        }
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }

    /**
     * 菜单删除操作
     *
     * @param menuDTO
     * @param request
     * @param response
     */
    @RequestMapping(value = "/delete")
    public void menuDelete(@RequestBody SysMenuDTO menuDTO, HttpServletRequest request, HttpServletResponse response) {
        Response resp = new Response();
        if (menuService.menuDelete(menuDTO)) {
            resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        } else {
            resp.setResponseCode(ResponseCodeEnum.PARAMETER_ERROR);
        }
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }
}
